package com.hudongwx.food.fargment;


import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;

import com.hudongwx.food.R;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

/**
 * 社区
 * A simple {@link Fragment} subclass.
 */
@ContentView(R.layout.fragment_community)
public class CommunityFragment extends Fragment implements View.OnClickListener{
    private View view;
    @ViewInject(R.id.community_tabLayout)
   private TabLayout tabLayout;
    @ViewInject(R.id.community_viewPager)
    private ViewPager viewPager;
    private RecommendFragment recommendFragment;
    private ThesunFragment thesunFragment;
    private MotionFragment motionFragment;
    private HealthFragment healthFragment;
    private LifeFragment lifeFragment;
    private FloatingActionButton fab,fabLocal,fabFavorite;
    private  int FAB_STATE=1;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (null == view) {
            view = x.view().inject(this, inflater, container);
//            viewPager = (ViewPager) view.findViewById(R.id.community_viewPager);
            viewPager.setAdapter(new Myadapter(getActivity().getSupportFragmentManager()));
            // Inflate the layout for this fragment
            tabLayout.setupWithViewPager(viewPager);
            fab= (FloatingActionButton) view.findViewById(R.id.fab);
            fabLocal= (FloatingActionButton) view.findViewById(R.id.fab_local);
            fabFavorite= (FloatingActionButton) view.findViewById(R.id.fab_favorite);

            fab.setOnClickListener(this);
            fabLocal.setOnClickListener(this);
            fabFavorite.setOnClickListener(this);

        }

        return view;
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){

            //            点击fab弹出上面两个FAB，通过FAB_STATE判断当前fab的打开状态
            case R.id.fab:
                //如果没有打开，则弹出
                if(FAB_STATE==1){
                    fab.setImageResource(R.mipmap.ic_launcher);
                    fabLocal.setVisibility(View.VISIBLE);
                    fabFavorite.setVisibility(View.VISIBLE);
                    FAB_STATE=0;
                    break;
                }
                //已经在打开状态，则关闭
                if(FAB_STATE==0){
                    fab.setImageResource(R.mipmap.ic_launcher);
                    fabLocal.setVisibility(View.GONE);
                    fabFavorite.setVisibility(View.GONE);
                    FAB_STATE=1;
                    break;
                }
                break;
            case R.id.fab_local:
                //弹出SnackBar，


                break;
            case R.id.fab_favorite:

                break;
        }

    }

    class Myadapter extends FragmentStatePagerAdapter {

        public Myadapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            if (null == recommendFragment) {
                recommendFragment = new RecommendFragment();
            }
            if (null == thesunFragment) {
                thesunFragment = new ThesunFragment();
            }
            if (null == healthFragment) {
                healthFragment = new HealthFragment();
            }
            if (null == motionFragment) {
                motionFragment = new MotionFragment();
            }
            if (null == lifeFragment) {
                lifeFragment = new LifeFragment();
            }
            switch (position) {
                case 0:
                    return recommendFragment;
                case 1:
                    return thesunFragment;
                case 2:
                    return healthFragment;
                case 3:
                    return motionFragment;
                case 4:
                    return lifeFragment;
            }
            return null;
        }

        @Override
        public int getCount() {
            return 5;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "推荐";
                case 1:
                    return "我来晒单";
                case 2:
                    return "饮食养生";
                case 3:
                    return "运动健身";
                case 4:
                    return "品质生活";
            }
            return null;
        }
    }

}
